package sensorrcp;

import java.util.List;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.ui.part.ViewPart;

import com.nml.sensors.SensorEntityManager;
import com.nml.sensors.Transmission;

public class TransmissionListView extends ViewPart {
	public TransmissionListView() {
	}

	public static final String ID = "SensorRCP.transmissionList";

	private TableViewer tableViewer;

	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	public void createPartControl(Composite parent) {
		parent.setLayout(new GridLayout(1, false));
		Button button = new Button(parent, SWT.PUSH);
		button.setText("Retrieve Transmission Table");
		button.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				List<Transmission> transmissionList = SensorEntityManager
						.getTransmissions();
				tableViewer.setInput(transmissionList.toArray());
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {

			}
		});
		tableViewer = new TableViewer(parent);
		Table table = tableViewer.getTable();
		table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

		tableViewer.setContentProvider(new ArrayContentProvider());

		TableViewerColumn nameColumn = new TableViewerColumn(tableViewer,
				SWT.NORMAL);
		nameColumn.setLabelProvider(new ColumnLabelProvider() {
			@Override
			public String getText(Object element) {
				Transmission transmission = (Transmission) element;
				return transmission.getRawBytes();
			}
		});
		nameColumn.getColumn().setResizable(true);
		nameColumn.getColumn().setWidth(400);
		nameColumn.getColumn().setText("Raw Bytes");

		List<Transmission> transmissionList = SensorEntityManager
				.getTransmissions();
		tableViewer.setInput(transmissionList.toArray());

	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {

	}
}